export default {
  components: {
    'single-Q': () => import('./cmms/single-Q'),
    'multiple-Q': () => import('./cmms/multiple-Q'),
    'stuff-Q': () => import('./cmms/stuff-Q'),
  },
  data() {
    return {
      scrollNum: 0,
      only_show_wrong_Q: this.$route.query.hasOwnProperty('wrong') ? true : false,
      paper_config: {
        paper_info: {},
        single_Q_info: null,
        multiple_Q_info: null,
        stuff_Q_info: null,
        single_Q_json: null,
        multiple_Q_json: null,
        stuff_Q_json: null,
      },
      lazy_load: {
        total: 0,
        current_length: 0,
        page: 1,
        size: 20, //speed
      },
      //sub-main
      Q_type_tabs: {
        tab_index: 0,
      },
      request_id: ""
    };
  },
  methods: {
    pxscroll(e) {
      let top = e.srcElement.scrollingElement.scrollTop; // 获取页面滚动高度
      this.scrollNum = top
    },


    // 智能练习
    aiJl() {
      const lazy_load = this.lazy_load;
      let obj = JSON.parse(sessionStorage.getItem("znObj"))
      obj.first_num = "",
        obj.second_num = "",
        obj.third_num = ""
      obj.xlid = this.$route.params.practise_id
      obj.sj_id = this.$route.params.paper_id

      this.$api.paper.aiJl(obj).then(rst => {
        if (rst.data.event !== '100') return;
        const paper_config = this.paper_config;
        const rst_data = rst.data;
        // rst_data.paper_id = paper_id; //todo 混合数据
        //保存试卷 , 试题基本信息
        paper_config.paper_info = rst_data;
        paper_config.single_Q_info = rst_data.d_info;
        paper_config.multiple_Q_info = rst_data.dx_info;
        paper_config.stuff_Q_info = rst_data.cl_info;
        //Q  main
        const Q_data = rst_data.st_list;
        const single_Q_json = [],
          multiple_Q_json = [],
          stuff_Q_json = [];
        //保存懒加载状态
        lazy_load.total = rst_data.num;
        lazy_load.current_length = Q_data.length;
        Q_data.map(v => {
          switch (v.tx) {
            case '1':
              single_Q_json.push(v);
              break;
            case '2':
              multiple_Q_json.push(v);
              break;
            case '3':
              v.yx_da = '';
              v.nr = v.nr.replace(/\n+/g, '<br/>');
              stuff_Q_json.push(v);
              break;
          }
        });
        paper_config.single_Q_json = single_Q_json;
        paper_config.multiple_Q_json = multiple_Q_json;
        paper_config.stuff_Q_json = stuff_Q_json;
      });
    },
    //每日一练
    startDaily() {
      console.log(this.$route.params,"/***************")
      const lazy_load = this.lazy_load;
      const paper_config = this.paper_config;
      const {
        product_id,
        paper_id,
        practise_id
      } = this.$route.params;
      let everyDa = JSON.parse(sessionStorage.getItem("everyData"))
      let obj = {
        sj_name: everyDa.sj_name,
        lb_id: everyDa.xm,
        token: this.$store.state.$user.token,
        kmlb: everyDa.km,
        xlid: practise_id,
        ctlx: "",
        sj_id: paper_id,
        date: this.day.date,
        week: this.day.w,
      }
      this.$api.paper.startDaily(obj).then(rst => {
        if (rst.data.event !== '100') return;
        const rst_data = rst.data;
        // rst_data.paper_id = paper_id; //todo 混合数据
        //保存试卷 , 试题基本信息
        paper_config.paper_info = rst_data;
        paper_config.single_Q_info = rst_data.d_info;
        paper_config.multiple_Q_info = rst_data.dx_info;
        paper_config.stuff_Q_info = rst_data.cl_info;
        //Q  main
        const Q_data = rst_data.st_list;
        const single_Q_json = [],
          multiple_Q_json = [],
          stuff_Q_json = [];
        //保存懒加载状态
        lazy_load.total = rst_data.num;
        lazy_load.current_length = Q_data.length;
        Q_data.map(v => {
          switch (v.tx) {
            case '1':
              single_Q_json.push(v);
              break;
            case '2':
              multiple_Q_json.push(v);
              break;
            case '3':
              v.yx_da = '';
              v.nr = v.nr.replace(/\n+/g, '<br/>');
              stuff_Q_json.push(v);
              break;
          }
        });
        paper_config.single_Q_json = single_Q_json;
        paper_config.multiple_Q_json = multiple_Q_json;
        paper_config.stuff_Q_json = stuff_Q_json;
        //载剩余数据
        lazy_load.current_length < lazy_load.total && this.zjst_json();
      });
    },




    // 章节练习
    zjst() {
      const lazy_load = this.lazy_load;
      const paper_config = this.paper_config;
      const {
        product_id,
        paper_id,
        practise_id
      } = this.$route.params;
      this.$api.paper.zjSjindex22({
        lb_id: product_id,
        zj_id: paper_id,
        xlid: practise_id,
        token: this.$store.state.$user.token,
        ctlx: this.only_show_wrong_Q ? 1 : '',
        page: lazy_load.page,
        size: lazy_load.size,
      }).then(rst => {
        if (rst.data.event !== '100') return;
        const rst_data = rst.data;
        rst_data.paper_id = paper_id; //todo 混合数据
        //保存试卷 , 试题基本信息
        paper_config.paper_info = rst_data;
        paper_config.single_Q_info = rst_data.d_info;
        paper_config.multiple_Q_info = rst_data.dx_info;
        paper_config.stuff_Q_info = rst_data.cl_info;
        //Q  main
        const Q_data = rst_data.st_list;
        const single_Q_json = [],
          multiple_Q_json = [],
          stuff_Q_json = [];
        //保存懒加载状态
        lazy_load.total = rst_data.num;
        lazy_load.current_length = Q_data.length;
        Q_data.map(v => {
          switch (v.tx) {
            case '1':
              single_Q_json.push(v);
              break;
            case '2':
              multiple_Q_json.push(v);
              break;
            case '3':
              v.yx_da = '';
              v.nr = v.nr.replace(/\n+/g, '<br/>');
              stuff_Q_json.push(v);
              break;
          }
        });
        paper_config.single_Q_json = single_Q_json;
        paper_config.multiple_Q_json = multiple_Q_json;
        paper_config.stuff_Q_json = stuff_Q_json;
        //载剩余数据
        lazy_load.current_length < lazy_load.total && this.zjst_json();
      });
    },

    zjst_json() {
      const paper_config = this.paper_config;
      const lazy_load = this.lazy_load;
      lazy_load.page++;
      //
      const {
        product_id,
        paper_id,
        practise_id
      } = this.$route.params;
      this.$api.paper.zjSjindex22({
        lb_id: product_id,
        zj_id: paper_id,
        xlid: practise_id,
        ctlx: this.only_show_wrong_Q ? 1 : '',
        page: lazy_load.page,
        token: this.$store.state.$user.token,
        size: lazy_load.size,
        DISABLED_LOADING: 1, //禁用loading
      }).then(rst => {
        if (rst.data.event !== '100') return;
        //copy
        const Q_data = rst.data.st_list;
        const single_Q_json = [],
          multiple_Q_json = [],
          stuff_Q_json = [];
        Q_data.map(v => {
          switch (v.tx) {
            case '1':
              single_Q_json.push(v);
              break;
            case '2':
              multiple_Q_json.push(v);
              break;
            case '3':
              v.nr = v.nr.replace(/\n+/g, '<br/>');
              stuff_Q_json.push(v);
              break;
          }
        });
        paper_config.single_Q_json.push(...single_Q_json);
        paper_config.multiple_Q_json.push(...multiple_Q_json);
        paper_config.stuff_Q_json.push(...stuff_Q_json);
        //d递归
        lazy_load.current_length < lazy_load.total && this.lazy_load_Q_json();
      });
    },

    init_Q_json() {
      const lazy_load = this.lazy_load;
      const paper_config = this.paper_config;
      const {
        product_id,
        paper_id,
        practise_id
      } = this.$route.params;
      this.$api.paper.detail_lazy({
        lb_id: product_id,
        sj_id: paper_id,
        xlid: practise_id,
        token: this.$store.state.$user.token,
        ctlx: this.only_show_wrong_Q ? 1 : '',
        page: lazy_load.page,
        size: lazy_load.size,
      }).then(rst => {
        if (rst.data.event !== '100') return;
        const rst_data = rst.data;
        rst_data.paper_id = paper_id; //todo 混合数据
        //保存试卷 , 试题基本信息
        paper_config.paper_info = rst_data;
        paper_config.single_Q_info = rst_data.d_info;
        paper_config.multiple_Q_info = rst_data.dx_info;
        paper_config.stuff_Q_info = rst_data.cl_info;
        //Q  main
        const Q_data = rst_data.st_list;
        const single_Q_json = [],
          multiple_Q_json = [],
          stuff_Q_json = [];
        //保存懒加载状态
        lazy_load.total = rst_data.num;
        lazy_load.current_length = Q_data.length;
        Q_data.map(v => {
          switch (v.tx) {
            case '1':
              single_Q_json.push(v);
              break;
            case '2':
              multiple_Q_json.push(v);
              break;
            case '3':
              v.yx_da = '';
              v.nr = v.nr.replace(/\n+/g, '<br/>');
              stuff_Q_json.push(v);
              break;
          }
        });
        paper_config.single_Q_json = single_Q_json;
        paper_config.multiple_Q_json = multiple_Q_json;
        paper_config.stuff_Q_json = stuff_Q_json;
        console.log(paper_config, "paper_config---paper_config")
        //载剩余数据
        lazy_load.current_length < lazy_load.total && this.lazy_load_Q_json();
      });
    },

    lazy_load_Q_json() {
      const paper_config = this.paper_config;
      const lazy_load = this.lazy_load;
      lazy_load.page++;
      //
      const {
        product_id,
        paper_id,
        practise_id
      } = this.$route.params;
      this.$api.paper.detail_lazy({
        lb_id: product_id,
        sj_id: paper_id,
        xlid: practise_id,
        ctlx: this.only_show_wrong_Q ? 1 : '',
        page: lazy_load.page,
        token: this.$store.state.$user.token,
        size: lazy_load.size,
        DISABLED_LOADING: 1, //禁用loading
      }).then(rst => {
        if (rst.data.event !== '100') return;
        //copy
        const Q_data = rst.data.st_list;
        const single_Q_json = [],
          multiple_Q_json = [],
          stuff_Q_json = [];
        Q_data.map(v => {
          switch (v.tx) {
            case '1':
              single_Q_json.push(v);
              break;
            case '2':
              multiple_Q_json.push(v);
              break;
            case '3':
              v.nr = v.nr.replace(/\n+/g, '<br/>');
              stuff_Q_json.push(v);
              break;
          }
        });
        paper_config.single_Q_json.push(...single_Q_json);
        paper_config.multiple_Q_json.push(...multiple_Q_json);
        paper_config.stuff_Q_json.push(...stuff_Q_json);
        //d递归
        lazy_load.current_length < lazy_load.total && this.lazy_load_Q_json();
      });
    },
    scroll2dom(dom_query) {
      window.$plus.scroll2dom(document.querySelector(dom_query));
    },
  },
  watch: {
    only_show_wrong_Q(newval) {
      //重新调接口，不要自己筛选数据·。
      // this.init_Q_json();
      let query
      if (newval) {
        query = {
          wrong: 'wrong'
        }
      } else {
        query = {}
      }
      this.$router.push({
        path: '',
        query: query
      })
    },
    $route: {
      handler(val) {
        this.$router.go(0)
      },
      deep: true
    }
  },
  beforeCreate() {
    //immersion_nav
    //   this.$store.state.use_immersion_nav = false;
    //   this.$is_auth(true);
  },
computed:{
  day() {
    let week = [
      "星期日",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六",

    ]
    let dObj = {}
    let d = new Date()
    let y = d.getFullYear()
    let m = ((d.getMonth() + 1) + "").padStart(2, 0)
    let r = ((d.getDate()) + "").padStart(2, 0)
    let w = d.getDay()
    let date = `${y}-${m}-${r}`
    dObj.date = date
    dObj.w = week[w]
    return dObj
  },
},
  created() {
    window.addEventListener("scroll", this.pxscroll)
    this.request_id = this.$route.params.st_lx + "" 
    if (this.$route.params.st_lx == "1") {
      this.init_Q_json();
    } else if (this.$route.params.st_lx == "2") {
      this.zjst()
    } else if (this.$route.params.st_lx == "3") {
      this.aiJl()
    } else if (this.$route.params.st_lx == "4") { //每日一练
      console.log(this.$route.params, "6666666666")
      this.startDaily()
    }

    console.log(this.$route.params.st_lx, "st_lx--st_lx")
  },
  beforeDestroy() {
    sessionStorage.removeItem("znObj")
    window.removeEventListener("scroll", this.pxscroll)
  },
};
